﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using TomanuExtensions.Utils;
using TomanuExtensions;

namespace AmiEmuLib.AmigaConfiguration.MountedDevicesConfig
{
    /// <summary>
    /// List of mounted devices.
    /// </summary>
    public class MountedDevices : XmlBase
    {
        /// <summary>
        /// Mounted devices list.
        /// </summary>
        public List<MountedDevice> List = new List<MountedDevice>();

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public MountedDevices(XmlReader a_reader)
            : base(a_reader)
        {
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            if (a_reader.IsEmptyElement)
            {
                a_reader.ReadStartElement(GetType().Name);
                return;
            }

            a_reader.ReadStartElement(GetType().Name);

            while (a_reader.NodeType != XmlNodeType.EndElement)
            {
                Type type = Type.GetType("AmiEmuLib.AmigaConfiguration.MountedDevicesConfig." +
                    a_reader.Name);
                List.Add((MountedDevice)Activator.CreateInstance(type, a_reader));
            }

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            foreach (var md in List)
                md.WriteXml(a_writer);

            a_writer.WriteEndElement();
        }
    }
}
